package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesClickInfo;
import com.lebang.domain.TaskRouse;
import com.lebang.example.CencesClickInfoExample;
import com.lebang.model.MyConfig;
import com.lebang.service.NewRouseService;
import com.lebang.service.cences.CencesClickInfoService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 签到应用详情
 *
 * @author hwq
 * @date 2020-04-16
 */
@Controller
@RequestMapping("/sys/cencesclickinfo")
public class CencesClickInfoController {
    @Autowired
    private CencesClickInfoService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> taskRouse = newRouseService.selectAll();
        taskRouse = newRouseService.checkRoot(taskRouse,session);
        model.addAttribute("list",taskRouse);
        return "/cencesclickinfo/list";
    }

    @GetMapping("/add")
    @DataSource("commonDB")
    public String add(Integer id, Model model) {
        CencesClickInfo vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesClickInfo();
        }
        model.addAttribute("item", vo);
        return "/cencesclickinfo/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData edit(CencesClickInfo vo) {
        if (vo.getId() != null) {
            baseService.updateById(vo);
        } else {
            vo.setAppId(MyConfig.getEnv());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> datalist(HttpSession session,
                                        @RequestParam Integer page,
                                        @RequestParam Integer limit,
                                        String appStore, String sdkKey, String appName) {
        CencesClickInfoExample example = new CencesClickInfoExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        CencesClickInfoExample.Criteria criteria = example.createCriteria();
        if(newRouseService.isRoot(session)){
            criteria.andSdkKeyNotIn(sdkList);
        }
        if(!TextUtil.isEmpty(appStore)){
            criteria.andAppStoreEqualTo(appStore);
        }
        if(!TextUtil.isEmpty(sdkKey)){
            criteria.andSdkKeyEqualTo(sdkKey);
        }
        if(!TextUtil.isEmpty(appName)){
            criteria.andAppNameEqualTo(appName);
        }
        criteria.andAppIdEqualTo(MyConfig.getEnv());
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", baseService.selectclickInfo(example));
        returnMap.put("count", baseService.countClickInfo(example));
        returnMap.put("code", 0);
        return returnMap;
    }


//    @ApiOperation(value = "分页")
//    @PostMapping("/list")
//    @ResponseBody
//    public JSONObject list(HttpServletRequest request,
//                           @RequestParam Integer page,
//                           @RequestParam Integer limit,
//                           String appStore, String sdkKey, String appName) {
//        CencesClickInfoExample example = new CencesClickInfoExample();
//        example.setOffset((page - 1) * limit.longValue());
//        example.setLimit(limit);
//        CencesClickInfoExample.Criteria criteria = example.createCriteria();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", 0);
//        jsonObject.put("data", baseService.page(example));
//        jsonObject.put("count", baseService.count(example));
//        return jsonObject;
//    }


}
